<?php
namespace app\index\validate;

use think\Validate;
use think\facade\Db;

/**
 * 用户验证
 */
class Index extends Validate
{
    protected $rule = [
        'category_id' => 'require|checkCategoryId',
        'detail_id' => 'require|checkDetailId'
    ];

    protected $message = [
        'category_id.require' => '分类参数错误',
        'detail_id.require' => '文章参数错误',
    ];

    protected $scene = [
        'category' =>  ['category_id'],
        'detail' => ['detail_id'],
    ];

    protected function checkCategoryId($value, $rule, $data)
    {
        if(!Db::name('category')->where('id', $value)->value('id')){
            return '分类不存在';
        }
        return true;
    }

    protected function checkDetailId($value)
    {
        if(!Db::name('article')->where('id', $value)->value('id')){
            return '文章不存在';
        }
        return true;
    }
}